const app =  getApp();
const { doLogin } = require('../../../common/utils/ws/wsxcx-opendata-sdk');

Page({

  data: {
    page: 1,
    url: '',  // 回放地址
    historyTimeStart: '', 
    historyTimeEnd: '', 
    recordList: [],
    vurl: '', // 实时监控摄像头地址
    abnorData: { imgSrc: '../../../common/static/images/abnor-02.png', text: '还没有相关监控数据！' },
    abnorD: false,
    endText: { isEnd: true, icon: '', text: '我是有底线的' },
    data: true,
    date: '',
  },

  onLoad: function(options) {
    let { id, vurl } = options;
    let that = this;
    this.setData({ id, vurl });
    wx.createLivePlayerContext('player'); // 创建 live-player 上下文 LivePlayerContext 对象

    // 初始化回放时间
    doLogin().then(res => {
      wx.setStorageSync('userInfo', { sessionid: res.sessionID, openid: res.openID });
      setTimeout(function() {
        that.getRecordList();
      }, 540);
    });
  },
  
  // 获取详情数据信息
  getRecordList() {
    let that = this;
    let sessionid = wx.getStorageSync('userInfo') && wx.getStorageSync('userInfo').sessionid || '';
    let {
      siteroot,
      uniacid,
      multiid,
      version,
    } = app.siteInfo;
    wx.request({
      url: siteroot + '?i=' + uniacid + '&t=' + multiid + '&v=' + version + '&from=wxapp&m=cm_monitor&c=entry&a=wxapp&do=index&state=v8uuid-' + sessionid,
      data: {
        op: 'getRecordList',
        camera_id: that.data.id,
        page: that.data.page,
        date: that.data.date,
      },
      success: (res) => {
        let { data } = res.data;
        data.length > 0 && that.setData({ recordList: that.data.recordList.concat(data) });
        that.setData({ abnorD: that.data.recordList.length > 0 ? false : true, data: !data ? true : false });
        wx.stopPullDownRefresh();
        wx.hideLoading();
      },
      fail: (res) => {
        console.log(res);
      },
    });
  },

  payhistory(e) {
    let { url } = e.currentTarget.dataset;
    this.setData({ url });
  },

  changeVL() {
    this.setData({ url: '' });
  },

  onReachBottom() {
    if (!this.data.abnorD && !this.data.data) { 
      this.setData({ page: this.data.page + 1 }); 
      this.getRecordList(); 
    }
    
  },

  // 开始时间
  bindDateChangeStart(e) {
    this.setData({ historyTimeStart: e.detail.value });
  },
  
  // 结束时间
  bindDateChangeEnd(e) {
    this.setData({ historyTimeEnd: e.detail.value });
  },

  // 搜索
  searchList(e) {
    let that = this;
    let startT = that.data.historyTimeStart ? new Date(that.data.historyTimeStart.replace(/-/g, '/')).getTime() / 1000 : '';
    let endT = that.data.historyTimeEnd ? new Date(that.data.historyTimeEnd.replace(/-/g, '/')).getTime() / 1000 : '';
    that.setData({
      page: 1, 
      recordList: [],
      date: startT && endT ? String(startT + ',' + endT) : '',
    });
    wx.showLoading({
      title: '数据获取中...',
      mask: true,
    });
    this.getRecordList();
  },
});